package com.zxct.wyserver.fragment;

import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.DecelerateInterpolator;
import android.view.animation.TranslateAnimation;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.Toast;

import com.socks.library.KLog;
import com.zxct.wyserver.R;
import com.zxct.wyserver.base.BaseFragment;
import com.zxct.wyserver.interactive.JavaScriptInterface;
import com.zxct.wyserver.util.StatusBarUtils;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * Created by lanyu on 2018/1/30.
 */

public class HomeFragment extends BaseFragment {

    @BindView(R.id.wb_home)
    WebView wbHome;
    Unbinder unbinder;
    @BindView(R.id.btn_pay)
    Button btnPay;

    @Override
    protected View initView() {
        View view = View.inflate(getContext(), R.layout.fragment_home, null);
        return view;
    }

    @Override
    public void onResume() {
        super.onResume();
        StatusBarUtils.setTranslucent(getActivity());
    }

    @Override
    protected void initDatas(View view) {
        super.initDatas(view);

        wbHome.getSettings().setJavaScriptEnabled(true); // 开启javascript支持
        wbHome.getSettings().setUseWideViewPort(true);//适配屏幕
        wbHome.getSettings().setDomStorageEnabled(true);//支持Html5标签
        wbHome.getSettings().setCacheMode(WebSettings.LOAD_DEFAULT);//优先使用缓存
        wbHome.getSettings().setSupportZoom(true);
        wbHome.getSettings().setAppCacheEnabled(true);
        wbHome.getSettings().setAllowFileAccess(true);//设置可以访问文件
        wbHome.getSettings().setDatabaseEnabled(true);
        wbHome.getSettings().setBuiltInZoomControls(true);


        // 设置js接口  第一个参数事件接口实例，第二个是实例在js中的别名，这个在js中会用到
        JavaScriptInterface JSInterface = new JavaScriptInterface(getActivity());
        wbHome.addJavascriptInterface(JSInterface, "JSInterface");

        //加载的动画
        final TranslateAnimation animation = new TranslateAnimation(
                Animation.RELATIVE_TO_SELF, 0.0f,
                Animation.RELATIVE_TO_SELF, 0.0f,
                Animation.RELATIVE_TO_SELF, -1.0f,
                Animation.RELATIVE_TO_SELF, 0.0f);
        //设置动画插值器 被用来修饰动画效果,定义动画的变化率
        animation.setInterpolator(new DecelerateInterpolator());
        //设置动画执行时间
        animation.setDuration(300);
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Thread.sleep(0);
                    getActivity().runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            wbHome.loadUrl("http://www.bluetopo.cn/WeChatPage/Page/bluetopo_test/Tenant/login");
                            wbHome.startAnimation(animation);
                        }
                    });
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }).start();

        //屏蔽掉长按事件
        wbHome.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                return true;
            }
        });

        //在同种分辨率的情况下,屏幕密度不一样的情况下,自动适配页面:
        DisplayMetrics dm = getResources().getDisplayMetrics();
        int scale = dm.densityDpi;
        if (scale == 240) {
            wbHome.getSettings().setDefaultZoom(WebSettings.ZoomDensity.FAR);
        } else if (scale == 160) {
            wbHome.getSettings().setDefaultZoom(WebSettings.ZoomDensity.MEDIUM);
        } else {
            wbHome.getSettings().setDefaultZoom(WebSettings.ZoomDensity.CLOSE);
        }

        //当一个新的url要在当前WebView进行加载的时候，这个方法给应用一个机会来控制url的处理。如果WebView没有setWebViewClient，
        // 则默认操作是WebView将询问Activity Manager获取合适的handler处理url。如果WebView设置了setWebViewClient，
        // 返回true代表当前应用来处理url，返回false则代表当前webview来处理url。如果http请求是POST方法，该方法将不会被调用。
        wbHome.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                if (url != "") {
                    view.loadUrl(url);   //在当前的webview中跳转到新的url
                    KLog.d(url);
                }
                return true;
            }
        });

        wbHome.setWebChromeClient(new WebChromeClient() {
            @Override
            public void onConsoleMessage(String message, int lineNumber, String sourceID) {
                KLog.d(lineNumber + "------" + message);
                super.onConsoleMessage(message, lineNumber, sourceID);
            }
        });
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO: inflate a fragment view
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        unbinder = ButterKnife.bind(this, rootView);
        return rootView;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    //微信支付
    @OnClick(R.id.btn_pay)
    public void onViewClicked() {

    }
}
